package com.dartfar.util;

import java.lang.reflect.Method;

import sun.reflect.misc.ReflectUtil;

public class EntityUtils {
	/**
	 * 判断实体是否为空 不为空返回为true
	 * 
	 * @param obj
	 * @return
	 */
	public static boolean isNotNull(Object obj) {
		if (obj == null) {
			return false;
		} else {
			return true;
		}
	}

	public static String getEntityIdValue(Object model, String idName) {
		String idValue = null;
		String getName = "get" + (idName.substring(0, 1)).toUpperCase()
				+ idName.substring(1, idName.length());
		idValue = invokeMethod(model, getName, null).toString();
		return idValue;
	}
	public static String getPropertyValue(Object model, String proName) {
		String proValue = null;
		String getName = "get" + (proName.substring(0, 1)).toUpperCase()
				+ proName.substring(1, proName.length());
		proValue = invokeMethod(model, getName, null).toString();
		return proValue;
	}
	public static Object invokeSetMethod(Object model,String keyName,Object[] args){
		String setName = "set" + (keyName.substring(0, 1)).toUpperCase()
				+ keyName.substring(1, keyName.length());
		model = invokeMethod(model, setName, args);
		return model;
	}
	public static Object invokeMethod(Object owner, String methodName,
			Object[] args) {
		Class ownerClass = owner.getClass();
		Class[] argsClass = null;
		if (args != null) {
			argsClass = new Class[args.length];
			for (int i = 0, j = args.length; i < j; i++) {
				if (args[i] != null) {
					if ("class java.lang.Boolean".equals(""
							+ args[i].getClass())) {
						argsClass[i] = boolean.class;
					} else {
						argsClass[i] = args[i].getClass();
					}
				} else {
					argsClass[i] = Object.class;
				}
			}
		}
		// 后的函数对象
		Method method = null;
		try {
			method = ownerClass.getMethod(methodName, argsClass);
		} catch (Exception e) { // 异常出现默认就为Object对象
			try {
				method = ownerClass.getMethod(methodName, Object.class);
			} catch (Exception e2) {
				System.out.println("获得类函数失败: " + e2);
			}
		}
		Object object = null;
		try {
			object = method.invoke(owner, args);
		} catch (Exception e) {
			System.out.println("执行函数[" + method.getName() + "]出错: " + e);
		}
		return object;
	}
}
